package com.TangoGames.box2d;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;

import com.TangoGames.panel.IPanelElement;
import com.TangoGames.sprite.Sprite;

public abstract class BaseActor implements IPanelElement {

	private ArrayList<IPanelElement> childrenElements = new ArrayList<IPanelElement>();
	
	protected WorldPanelState worldPanel = null;
	
	protected Body worldBody = null;
	
	protected Sprite sprite;
	
	private boolean markToRemove = false;
	
	public boolean isMarkToRemove() {
		return markToRemove;
	}

	public void setMarkToRemove(boolean markToRemove) {
		this.markToRemove = markToRemove;
	}

	private Point position = new Point(0,0);  

	private Point drawOffSet = new Point(0,0);
	
	public void setPosition(Point position) { this.position=position; }
	
	public Point getPosition() { return position; }

	public void setDrawOffSet(Point drawOffSet) { this.drawOffSet=drawOffSet; }
	
	public Point getDrawOffSet() { return drawOffSet; }

	public Body getWorldBody() { return worldBody; }
	
	protected BaseActor( WorldPanelState worldPanel, Point position) {
		this.worldPanel	= worldPanel;
		this.position	= position;
	}
	
	public BaseActor( WorldPanelState worldPanel, Sprite sprite, Point position,  boolean isCircle) {
		this.worldPanel	= worldPanel;
		this.sprite 	= sprite;
		this.position	= position;
		CreateBodyActor(sprite, position,  isCircle);
	}

	/**
	 * 
	 * @param sprite
	 * @param position
	 * @param isCircle
	 */
	protected void CreateBodyActor(Sprite sprite, Point position,  boolean isCircle){

		this.sprite 	= sprite;
		this.position	= position;
		
		Rect rect = new Rect(position.x, position.y, position.x + sprite.getWidth(),  position.y + sprite.getHeight() );
		
		if (!isCircle) {
			this.worldBody 	=  worldPanel.createActor(this, rect);
		}
		else {
			this.worldBody 	=  worldPanel.createActor(this,  position, sprite.getWidth()/2);
		}
		
	}
	
	@Override
	public Collection<IPanelElement> getChildrenElements() {
		return childrenElements;
	}

	@Override
	public IPanelElement getParentElement() {
		return worldPanel;
	}

	@Override
	public boolean isDrawable() {
		return worldPanel.isDrawActors();
	}

	@Override
	public void Draw(Canvas canvas) {
		sprite.draw(canvas);
	}

	@Override
	public void Update(long gameTime) {
		if (!markToRemove) {
			Vec2 screenpos = new Vec2();
			worldPanel.getViewportTransform().getWorldToScreen(worldBody.getPosition(), screenpos);
			sprite.setX((int)screenpos.x);
			sprite.setY((int)screenpos.y);
			sprite.setRotate( (float) (360 - Math.toDegrees( worldBody.getAngle() ) ) ) ;
		}
	}

	/**
	 * 
	 */
	final public void Destroy(){
		worldPanel.getSimulator().getWorld().destroyBody(worldBody);
	}
	
}
